﻿using PersonalWebsite.Domain.Abstract.DomainEvent;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace PersonalWebsite.Domain.Abstract.Entities;

public abstract class Entity : IEntity
{
    public abstract object[] GetKeys();

    #region 领域事件

    private List<IDomainEvent> _domainEvents { set; get; }

    [NotMapped]
    [JsonIgnore]
    public IReadOnlyCollection<IDomainEvent> DomainEvents { get { return _domainEvents?.AsReadOnly(); } }

    public void AddDomainEvent(IDomainEvent domainEvent)
    {
        if (_domainEvents == null)
            _domainEvents = new List<IDomainEvent>();

        _domainEvents.Add(domainEvent);
    }

    public void RemoveDomainEvent(IDomainEvent domainEvent)
    {
        if (_domainEvents != null)
        {
            _domainEvents.Remove(domainEvent);
        }
    }

    public void ClearDomainEvent() => _domainEvents?.Clear();

    #endregion

}

public abstract class Entity<TKey> : Entity, IEntity<TKey>
{
    public TKey Id { set; get; }

    public override object[] GetKeys()
    {
        return new object[] { Id };
    }
}
